package cn.credit.loan.controller;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * 权限管理员控制类
 * @author 
 *
 */
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.credit.loan.entity.Authority;
import cn.credit.loan.service.authority.AuthorityService;
@Controller
public class AuthorityController {
	//日志输出
	private Logger logger = Logger.getLogger(AuthorityController.class);
	//注入
	@Autowired
	private AuthorityService authorityService;
	
	/**
	 * 权限管理员登录
	 * @param authorityName
	 * @param authorityPwd
	 * @param model
	 * @return
	 */
	@RequestMapping("/adminlogin")
	public ModelAndView logins(@RequestParam("adminName")String authorityName,
			@RequestParam("adminPwd")String authorityPwd,ModelMap model,HttpSession session)
	{
		try {
			Authority authority=authorityService.login(authorityName, authorityPwd);
			if(authority!=null){
				if(!authority.getAuthorityPwd().equals(authorityPwd)){
					model.addAttribute("errorpwd", "密码输入不正确");
					return new ModelAndView("after/login","roleModel",model);
				}
				session.setAttribute("authority", authority);
				session.setAttribute("role",1);
				session.setAttribute("rolelogin", "管理员");
				return new ModelAndView("after/index","roleModel",session);
			}model.addAttribute("errorName", "管理员不存在");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView("after/login","",model);
	}
	
	/**
	 * 权限管理员添加
	 * @param authority
	 * @param model
	 * @return
	 */
	@RequestMapping("/authorityadd")
	public String authorityAdd(Authority authority,ModelMap model)
	{
		try {
			authority.setAuthorityName("测试001");
			boolean authoritys=authorityService.authorityAdd(authority);
			if(authoritys)
			{
				model.addAttribute("authority", authority);
				return "AuthorityAdd";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	

	/**
	 * 权限管理员更新
	 * @param authority
	 * @param model
	 * @return
	 */
	@RequestMapping("/authorityupdate")
	public String authorityUpdate(Authority authority,ModelMap model)
	{
		try {
			authority.setAuthorityName("测试001");
			authority.setAuthorityPwd("123456");
			boolean authoritys=authorityService.authorityUpdate(authority);
			if(authoritys)
			{
				model.addAttribute("authority", authority);
				return "AuthorityUpdate";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	/**
	 * 权限管理员删除
	 * @param authorityId
	 * @param model
	 * @return
	 */
	@RequestMapping("/authoritydelete")
	public String authorityDelete(int authorityId,ModelMap model)
	{
		try {
			boolean authorityIds=authorityService.authorityDelete(authorityId);
			if(authorityIds)
			{
				model.addAttribute("authorityId", authorityId);
				return "AuthorityDelete";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
}
